package com.mida.bms.report.controller;


import com.mida.bms.report.entity.DocStructTemplateAuth;
import com.mida.bms.report.entity.DocStructTemplateAuthSpace;
import com.mida.bms.report.service.DocStructTemplateAuthSpaceService;
import com.mida.bms.report.vo.DocStructTemplateAuthBatchVo;
import com.mida.bms.report.vo.DocStructTemplateAuthSpaceVo;
import com.mida.bms.report.vo.PageResultVo;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;


import java.util.List;

import static com.mida.tool.common.result.BaseResult.buildActionWarn;

/**
 * @author ：wang xiao le
 * @description：结构化模板授权空间
 * @date ：2021/3/30 10:10
 */
@RestController
@RequestMapping("/docStructTemplateAuth")
@CrossOrigin
@Slf4j
public class DocStructTemplateAuthSpaceController {

    @Autowired
    private DocStructTemplateAuthSpaceService docStructTemplateAuthSpaceService;

    /**
     * 1、添加结构化模板报告授权空间
     *
     * @param docStructTemplateAuthSpaceVo   结构化模板报告授权空间Vo
     * @return
     */
    @PostMapping(value = "add",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ActionResult add(@RequestBody DocStructTemplateAuthSpaceVo docStructTemplateAuthSpaceVo) throws Exception {

        ActionResult result = new ActionResult();
        try {
            return docStructTemplateAuthSpaceService.add(docStructTemplateAuthSpaceVo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("添加结构化模板报告授权空间失败");
            return result;
        }
    }


    /**
     * 2、更新结构化模板报告授权空间
     *
     * @param docStructTemplateAuthSpaceVo
     * @return
     */
    @PostMapping("update")
    public ActionResult update( @RequestBody  DocStructTemplateAuthSpaceVo docStructTemplateAuthSpaceVo) throws Exception {

        ActionResult result = new ActionResult();
        try {
            return docStructTemplateAuthSpaceService.update(docStructTemplateAuthSpaceVo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("更新结构化模板报告授权空间失败");
            return result;
        }
    }

    /**
     * 3、删除结构化模板报告授权空间
     *
     * @param stTempAuthSpSoid 结构化模板报告授权空间soid
     * @return null
     */
    @DeleteMapping(value = "/delete")
    public ActionResult delete(@RequestParam(value = "stTempAuthSpSoid") long stTempAuthSpSoid) {
        ActionResult result = new ActionResult();
        try {
            return docStructTemplateAuthSpaceService.delete(stTempAuthSpSoid);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("删除结构化模板报告授权空间失败");
            return result;
        }
    }

    /**
     * 4、获取单个结构化模板报告授权空间
     *
     * @param stTempAuthSpSoid 结构化模板报告授权空间soid
     * @return
     */
    @GetMapping("get")
    public ListResult<DocStructTemplateAuthSpaceVo> get(@RequestParam(value = "stTempAuthSpSoid") long stTempAuthSpSoid) {

        ListResult result = new ListResult();
        try {
            return docStructTemplateAuthSpaceService.get(stTempAuthSpSoid);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("获取单个结构化模板报告授权空间失败");
            return result;
        }
    }

    /**
     * 5、分页查询结构化模板报告授权空间
     * @param searChData  搜索关键字
     * @param currentPage 第几页
     * @param pageSize    每页显示数
     * @return
     */
    @ApiOperation(value = "分页查询结构化模板报告授权空间", notes = "分页查询结构化模板报告授权空间")
    @PostMapping(value = "/findPage",produces = { "application/json;charset=UTF-8" })
    public ListResult<PageResultVo<DocStructTemplateAuthSpaceVo>> findPage(@RequestParam(value = "searchData") String searChData,
                                                                           @RequestParam(value = "currentPage") Long currentPage,
                                                                           @RequestParam(value = "pageSize") Long pageSize) {

        ListResult result = new ListResult();
        try {
            return docStructTemplateAuthSpaceService.findPage(searChData, currentPage, pageSize);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("分页查询结构化模板报告授权空间失败");
            return result;
        }

    }
    /**
     * 6、结构化模板报告授权空间名称重复性校验
     */
    @GetMapping(value = "/checkName")
    public ActionResult checkName(@RequestParam(value = "name", required = true) String name,
                                  @RequestParam(value = "stTempAuthSpSoid", required = false) Long stTempAuthSpSoid) {


        ActionResult result = new ActionResult();
        try {
            return docStructTemplateAuthSpaceService.checkName(name,stTempAuthSpSoid);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("结构化模板报告授权空间名称重复性校验失败");
            return result;
        }

    }

    /**
     * 7、更改结构化模板报告授权空间状态
     *
     * @param stTempAuthSpSoid 授权空间soid
     * @param status       状态soid
     * @return
     */
    @GetMapping("/updateStatus")
    public ActionResult updateStatus(@RequestParam(value = "status", required = true) Long status,
                                     @RequestParam(value = "stTempAuthSpSoid", required = false) Long stTempAuthSpSoid) {


        ActionResult result = new ActionResult();
        try {
            return docStructTemplateAuthSpaceService.updateStatus(status, stTempAuthSpSoid);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("更改结构化模板报告授权空间状态失败");
            return result;
        }
    }

    /**
     * 8、结构化模板报告授权空间下拉(查询所有授权空间)
     * @return
     */
    @GetMapping("/findAll")
    public ListResult<List<DocStructTemplateAuthSpace>> findAll(@RequestParam(value = "searchData") String searChData,@RequestParam(value = "hospitalSoid") Long hospitalSoid){
        ListResult result = new ListResult();
        try {
            return docStructTemplateAuthSpaceService.findAll(searChData,hospitalSoid);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("查询所有授权空间失败");
            return result;
        }
    }

    /**
     * 9、根据授权空间soid查询授权空间基本信息
     * @param stTempAuthSpSoid 授权空间soid
     * @return 授权空间基本信息
     */
    @GetMapping("selectByAuthSpSoid")
    @ResponseBody
    public ListResult<DocStructTemplateAuthSpaceVo> selectByAuthSpSoid(@RequestParam(value = "stTempAuthSpSoid") long stTempAuthSpSoid) {

        ListResult result = new ListResult();
        try {
            return docStructTemplateAuthSpaceService.selectByAuthSpSoid(stTempAuthSpSoid);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("查询授权空间基本信息失败");
            return result;
        }
    }

    /**
     * 10. 批量插入模板授权空间业务处理
     * @param
     * @return
     */
    @PostMapping(value = "/addDocStructTemplateAuthBatch")
    @ResponseBody
    public ActionResult addDocStructTemplateAuthBatch(@RequestBody List<DocStructTemplateAuth> listDocStructTemplateAuth){
        ActionResult result = new ActionResult();
        try {
            return docStructTemplateAuthSpaceService.addDocStructTemplateAuthBatch(listDocStructTemplateAuth);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            buildActionWarn(result, "批量插入模板授权空间业务处理失败，请联系管理员");
        }
        return result;
    }

}
